// 
//  Main.cs
//  
//  Author:
//   Nikita Tsukanov <keks9n@gmail.com>
// 
//  Copyright (c) 2011 Nikita Tsukanov
// 
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
// 
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// 


using System;
using Gtk;
using System.Runtime.InteropServices;

namespace SkypeTab
{
	class Globals
	{
		const int PR_SET_NAME = 15;
		[System.Runtime.InteropServices.DllImport("libc")]
		static extern int prctl(int option, string arg2,IntPtr arg3 , IntPtr arg4, IntPtr arg5);
		[System.Runtime.InteropServices.DllImport("libgtk-x11-2.0.so.0")]
		public static extern IntPtr gdk_x11_display_get_xdisplay(IntPtr GdkDisplay);
		public static bool SetProcessName (string name)
		{
			return prctl (PR_SET_NAME, name, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero) == 0;
		}
		
		
		
		public static void Main (string[] args)
		{
			//Config.Init();
			X11.X.XInitThreads ();
			Application.Init ("SkypeTab", ref args);
			X11.X.ConnectToDisplay (gdk_x11_display_get_xdisplay (Gdk.Display.Default.Handle));
			X11.X.Init ();
			SetProcessName ("skypetab");

			
			MainWindow win = new MainWindow ();
			win.Show ();
			Application.Run ();
		}
	public static class Config
	{
		public class ConfigFile:System.Collections.Generic.List<ConfigKeyValue<ConfigSection>>
		{
			public ConfigSection GetSection (string Section)
			{
				Section = Section.ToLower ();
				foreach (var entry in this)
				{
					if (entry.Key == Section)
					{
						return entry.Value;
					}
				}
				var newsect = new ConfigSection ();
				Add (new ConfigKeyValue<ConfigSection> (Section, newsect));
				return newsect;
			}
		}
		public class ConfigKeyValue<T>
		{
			public string Key;
			public T Value;
			public ConfigKeyValue ()
			{
			}
			public ConfigKeyValue (string key, T value)
			{
				Key = key;
				Value = value;
			}
		}
		public class ConfigSection:System.Collections.Generic.List<ConfigKeyValue<object>>
		{
			public bool TryGet (string Key, out object Value)
			{
				Key = Key.ToLower ();
				foreach (var entry in this)
				{
					if (entry.Key == Key)
					{
						Value = entry.Value;
						return true;
					}
				}
				Value = null;
				return false;
			}
			public void SetValue (string Key, object Value)
			{
				Key = Key.ToLower ();
				foreach (var entry in this)
				{
					if (entry.Key == Key)
					{
						entry.Value = Value;
						return;
					}
				}
				Add (new ConfigKeyValue<object>(Key, Value));
			}
		}
		private static ConfigFile m_Config=new ConfigFile();
		private static string Path=System.Environment.GetEnvironmentVariable("HOME")+"/.config/skypetab";
		static System.Xml.Serialization.XmlSerializer Serializer = new System.Xml.Serialization.XmlSerializer (m_Config.GetType ());
		public static void Init ()
		{
			
			System.IO.FileStream stream;
			try
			{
				stream = System.IO.File.OpenRead (Path);
			}
			catch
			{
				return;
			}
			
			m_Config = (ConfigFile)Serializer.Deserialize (stream);
			stream.Close ();
		}
		public static void Save ()
		{
			if(System.IO.File.Exists(Path))
				System.IO.File.Delete(Path);
			var stream = System.IO.File.OpenWrite (Path);
			Serializer.Serialize (stream, m_Config);
			stream.Close ();
		}
		
		public static void SetValue (string Section, string Key, object Value)
		{

			m_Config.GetSection (Section).SetValue (Key, Value);
			Save ();
		}
		
		public static T GetValue<T> (string Section, string Key, T Default)
		{
			object value;
			if (m_Config.GetSection (Section).TryGet (Key, out value))
			{

				return (T)value;
			}
			return Default;
		}
		
		
		
	}

	}
}

